package com.lightboat.web.controller.crm;

import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.page.TableDataInfo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.common.utils.PageUtils;
import com.lightboat.common.utils.poi.ExcelUtil;
import com.lightboat.system.domain.CrmProductStockRecord;
import com.lightboat.system.service.ICrmProductStockRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品库存记录Controller
 *
 * @author lightboat
 * @date 2025-09-12
 */
@Tag(name = "商品库存记录")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/product/stock/record")
public class CrmProductStockRecordController extends BaseController {

    private final ICrmProductStockRecordService crmProductStockRecordService;

    /**
     * 查询商品库存记录列表
     */
    @Operation(summary = "查询商品库存记录列表")
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo<CrmProductStockRecord> list(CrmProductStockRecord crmProductStockRecord) {
        PageUtils.<CrmProductStockRecord>chainOrderBy().orderByDesc(CrmProductStockRecord::getCreateTime).apply();
        startPage();
        List<CrmProductStockRecord> list = crmProductStockRecordService.selectCrmProductStockRecordList(crmProductStockRecord);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 导出商品库存记录列表
     */
    @Operation(summary = "导出商品库存记录列表")
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "商品库存记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmProductStockRecord crmProductStockRecord) {
        List<CrmProductStockRecord> list = crmProductStockRecordService.selectCrmProductStockRecordList(crmProductStockRecord);
        ExcelUtil<CrmProductStockRecord> util = new ExcelUtil<CrmProductStockRecord>(CrmProductStockRecord.class);
        util.exportExcel(response, list, "商品库存记录数据");
    }

    /**
     * 获取商品库存记录详细信息
     */
    @Operation(summary = "获取商品库存记录详细信息")
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(crmProductStockRecordService.selectCrmProductStockRecordById(id));
    }

    /**
     * 新增商品库存记录
     */
    @Operation(summary = "新增商品库存记录")
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "商品库存记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmProductStockRecord crmProductStockRecord) {
        return toAjax(crmProductStockRecordService.insertCrmProductStockRecord(crmProductStockRecord));
    }

    /**
     * 修改商品库存记录
     */
    @Operation(summary = "修改商品库存记录")
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "商品库存记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmProductStockRecord crmProductStockRecord) {
        return toAjax(crmProductStockRecordService.updateCrmProductStockRecord(crmProductStockRecord));
    }

    /**
     * 删除商品库存记录
     */
    @Operation(summary = "删除商品库存记录")
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "商品库存记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(crmProductStockRecordService.deleteCrmProductStockRecordByIds(ids));
    }
}
